var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
var user = require('../../../services/user.js');
var app = getApp();

Page({
    data: {
        userInfo: {},
        hasMobile:'no',
        hasLogin:false
    },
    onLoad: function (options) {
        // 页面初始化 options为页面跳转所带来的参数
        console.log(app.globalData)
    },
    onReady: function () {
    
    },
    onShow: function () {
// console.log(this.data.hasLogin,this.data.hasMobile)
        let userInfo = wx.getStorageSync('userInfo');
        let token = wx.getStorageSync('token');
        let phoneNum = wx.getStorageSync('YBBPhoneNum')
        // 页面显示
        if (userInfo && token) {
            app.globalData.userInfo = userInfo;
            app.globalData.token = token;
            this.setData({
              hasLogin:true
            })
        }
      if (phoneNum){
        this.setData({
          hasMobile:'yes'
        })
      }
console.log('hasLogin：',this.data.hasLogin,this.data.hasMobile)
        this.setData({
            userInfo: app.globalData.userInfo,
        });

    },
    onHide: function () {
        // 页面隐藏

    },
    onUnload: function () {
        // 页面关闭
    },
    bindPhone(e){
      console.log('')
      console.log(e)
      var that =this
      wx.getStorage({
        key: 'YBBsessionKey',
        success: function(res) {
          console.log(res)
          var sessionKey = res.data
          util.request(api.BindPhone, { sessionkey: sessionKey, encrypdata: e.detail.encryptedData,ivdata:e.detail.iv}, 'GET').then((res)=>{
            console.log(res)
            if(res.errno==0){
              that.setData({
                hasMobile:true
              })
              app.globalData.phoneNumber = res.data.phoneNumber
              wx.setStorage({
                key: 'YBBPhoneNum',
                data: res.data.phoneNumber,
              })
            }
          })
        },
        fail:function(err){
          wx.showToast({
            title: '请先登录',
            icon:'none',
            mask:true
          })
        }
      })
    },
    goPhone(){
      if(!this.data.hasLogin){
        wx.showToast({
          title: '请先登录',
          icon:'none',
          mask:true
        })
        return
      }
      wx.navigateTo({
        url: '/pages/auth/mobile/mobile',
      })
      console.log('转到绑定手机号页面')
    },
    
    bindGetUserInfo(e) {
      
      let userInfo = wx.getStorageSync('userInfo');
      let token = wx.getStorageSync('token');
      if (userInfo && token) {
        return;
      }
      if (e.detail.userInfo){
        wx.showLoading({
          title: '登录中...',
        })
        //用户按了允许授权按钮
        user.loginByWeixin(e.detail).then(res => {
            this.setData({
                userInfo: res.data.userInfo,
                hasLogin:true
            });
            app.globalData.userInfo = res.data.userInfo;
            app.globalData.token = res.data.token;
          if (res.data.hasMobile=='no'){
              this.setData({
                hasMobile:'no'
              })
              app.globalData.sessionKey = res.data.sessionKey
              wx.setStorage({
                key: 'YBBsessionKey',
                data: res.data.sessionKey,
              })
            }else{
              this.hasMobile='yes'
              wx.setStorage({
                key: 'YBBphoneNumber',
                data: res.data.phoneNumber,
              })
            }
            wx.showToast({
              title: '登录成功',
            })
        }).catch((err) => {
            console.log(err)
        });
        wx.hideLoading()
      } else {
        //用户按了拒绝按钮
        wx.showModal({
            title: '警告通知',
            content: '您点击了拒绝授权,将无法正常显示个人信息,点击确定重新获取授权。',
            success: function (res) {
                if (res.confirm) {
                    wx.openSetting({
                        success: (res) => {
                            if (res.authSetting["scope.userInfo"]) {////如果用户重新同意了授权登录
                                user.loginByWeixin(e.detail).then(res => {
                                    this.setData({
                                        userInfo: res.data.userInfo
                                    });
                                    app.globalData.userInfo = res.data.userInfo;
                                    app.globalData.token = res.data.token;
                                }).catch((err) => {
                                    console.log(err)
                                });
                            }
                        }
                    })
                }
            }
        });
      }
    },
    exitLogin: function () {
        wx.showModal({
            title: '',
            confirmColor: '#b4282d',
            content: '退出登录？',
            success: function (res) {
                if (res.confirm) {
                    wx.removeStorageSync('token');
                    wx.removeStorageSync('userInfo');
                    wx.switchTab({
                        url: '/pages/index/index'
                    });
                }
            }
        })

    }
})